<!DOCTYPE html>
<html lang="ch-ZN">
<head>
    <meta charset="UTF-8">
    <title>slot 插槽</title>
</head>

<body>

<div id="app">

    <!--    <p>列表书籍</p>-->

    <!--    <ul>-->
    <!--        <li>JAVA</li>-->
    <!--        <li>Linux</li>-->
    <!--        <li>Python</li>-->
    <!--    </ul>-->
    <todo>
        <to-title slot="to-title" :title="title"></to-title>
        <to-content slot="to-content" v-for="(item,index) in todoItems" :content="item" :index="index"
                    @remove="removeItem(index)"
                    :key="index"
        ></to-content>
    </todo>

</div>

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<!--<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>-->
<script>
    // slot: 插槽
    Vue.component('todo', {
        template: '<div>' +
            '<slot name="to-title"></slot>' +
                '<ul>' +
                    '<slot name="to-content"></slot>' +
                '</ul>' +
            '</div>'
    });

    Vue.component('to-title', {
        props: ['title'],
        template: '<div> {{title}}</div>'
    });

    Vue.component('to-content', {
        props: ['content','index'],
        //只能绑定当前组队的方法
        template: '<li>{{index}}-----{{content}} <button @click="deleteItem">删除</button></li>',
        methods: {
            deleteItem: function (index) {
                //自定义事件
                this.$emit('remove',index);
                // alert('删除成功');
            }
        }
    });

    var vm = new Vue({
        el: '#app',
        data: {
            title: '书籍列表',
            todoItems: ['Java', '前端', 'C++']
        },
        methods: {
            removeItem: function (index) {
                console.log("删除了"+this.todoItems[index])
                this.todoItems.splice(index,1);
            }
        }

    })

</script>
</body>
</html>